package com.hzy.fivepalace.wx.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzy.fivepalace.common.page.PageUtils;
import com.hzy.fivepalace.common.page.TableDataInfo;
import com.hzy.fivepalace.db.domain.DtsGoods;
import com.hzy.fivepalace.db.domain.DtsGroupOpenList;
import com.hzy.fivepalace.db.domain.DtsGroupProduct;
import com.hzy.fivepalace.db.service.DtsGoodsService;
import com.hzy.fivepalace.db.service.DtsGroupOpenListService;
import com.hzy.fivepalace.db.service.DtsGroupProductService;
import com.hzy.fivepalace.wx.controller.base.BaseController;
import com.hzy.fivepalace.wx.service.WxGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品类目服务
 */
@RestController
@RequestMapping("/wx/group")
@Validated
@Api(tags = "商品服务")
public class WxGroupController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WxGroupController.class);

    @Resource
    private DtsGroupProductService groupProductService;
    @Resource
    private DtsGroupOpenListService groupOpenListService;
    @Resource
    private WxGoodsService wxGoodsService;
    @Resource
    private DtsGoodsService goodsService;


    @Value("${sys-config.imgUrl}")
    private String imgUrl;

    /**
     * 拼团列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "首页类目列表")
    public TableDataInfo getCategoryList(@Validated DtsGroupProduct mallGroupProduct) {
        QueryWrapper<DtsGroupProduct> qw = new QueryWrapper<>();
        if(mallGroupProduct.getLinkId() !=null){
            qw.eq("link_id",mallGroupProduct.getLinkId());
        }
        if(mallGroupProduct.getState() !=null){
            qw.eq("state",mallGroupProduct.getState());
        }
        if(StrUtil.isNotEmpty(mallGroupProduct.getTitle())){
            qw.like("title", mallGroupProduct.getTitle());
        }
        qw.orderByDesc("id");

        TableDataInfo<DtsGroupProduct> td = PageUtils.buildDataInfo(groupProductService.page(PageUtils.buildPage(),qw));
        List<DtsGroupProduct> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
            for (DtsGroupProduct groupProduct : list){
                DtsGoods goods = goodsService.findById(groupProduct.getLinkId());
                groupProduct.setGoods(goods);

                if(StrUtil.isNotEmpty(groupProduct.getActiveImg())){
                    groupProduct.setActiveImgPath(imgUrl + groupProduct.getActiveImg());
                }
                groupProduct.setIsHasGroup(0);
                QueryWrapper<DtsGroupOpenList> qwOpen = new QueryWrapper<>();
                qwOpen.eq("group_id", groupProduct.getId());
                qwOpen.eq("state", 0);
                qwOpen.last("limit 1");
                DtsGroupOpenList groupOpenList = groupOpenListService.getOne(qwOpen);
                if(groupOpenList != null){
                    groupProduct.setIsHasGroup(1);
                }
                groupProduct.setGroupOpenList(groupOpenList);
            }
        }
        return td;
    }
}
